import * as echarts from '../../ec-canvas/echarts';
const app = getApp();
var fund_cn_name = '';
var fund_id_g = '000001';
var lastdays_g = '30';

// 127.0.0.1:9999/api/v1/mysql_qps_monitor
function initChart(canvas, width, height, dpr) {
  const chart = echarts.init(canvas, null, {
    width: width,
    height: height,
    devicePixelRatio: dpr // new
  });
  var timeList = []
  var navList = []
  canvas.setChart(chart);
  wx.request({
    method: 'GET',
    url: 'http://192.168.0.105:8002/api/transactionrecord-list/'+ fund_id_g + '/'+ lastdays_g +'/0/',
    header: {
      'Content-Type': 'application/json'
    },
    success: function(res) {
      var obj = JSON.parse(res.data)
      for(var i = 0; i < obj.data.length; i ++) {
        // console.log(obj.data[i])
        timeList.push(obj.data[i].date)
        navList.push(obj.data[i].average_nav)
      }
      var option = {
        title: {
          text: fund_cn_name,
          left: 'center'
        },
        color: ["#37A2DA"],
        legend: {
          data: ['A'],
          top: 50,
          left: 'center',
          backgroundColor: 'white',
          z: 100
        },
        grid: {
          containLabel: true
        },
        tooltip: {
          show: true,
          trigger: 'axis'
        },
        xAxis: {
          type: 'category',
          boundaryGap: false,
          data: timeList,
          // show: false
        },
        yAxis: {
          x: 'center',
          type: 'value',
          splitLine: {
            lineStyle: {
              type: 'dashed'
            }
          }
          // show: false
        },
        series: [{
          name: '基金净值',
          type: 'line',
          smooth: true,
          data: navList,
        }]
      };
      chart.setOption(option);
      return chart;
    },
    fail: function(){
    }
  })
  return chart;
}

Page({
  onLoad: function (options) {
    this.setData({
      fund_id: options.id,
      cn_name: options.cn_name,
      lastdays: options.lastdays_cur,
    })
    fund_cn_name = options.cn_name
    fund_id_g = options.id
    let this_ = this
    wx.request({
      method: 'GET',
      url: 'http://192.168.0.105:8002/api/transactionrecord-list/'+ fund_id_g +'/'+ lastdays_g+'/1/',
      header: {
        'Content-Type': 'application/json'
      },
      success: function(res) {
        var obj = JSON.parse(res.data)
        this_.setData({
          fundDatas: obj.data,
        })
      },
      fail: function(){
      }
    })
  },
  onShareAppMessage: function (res) {
    return {
      title: 'ECharts 可以在微信小程序中使用啦！',
      path: '/pages/index/index',
      success: function () { },
      fail: function () { }
    }
  },

  bindLastDaysChange: function(e) {
    console.log('picker发送选择改变，携带值为', this.data.daysitem[e.detail.value])
    this.setData({
      index: e.detail.value,
      lastdays: this.data.daysitem[e.detail.value],
    })
    lastdays_g = this.data.daysitem[e.detail.value]
    let options = getCurrentPages().pop().options
    let params = Object.keys(options).map(item=>{
      return (item) + "=" + (options[item]);
    }).join("&");
    wx.reLaunch({ url: "/" + getCurrentPages().pop().route +"?"+ params + "&lastdays_cur=" + lastdays_g});
  },

  data: {
    fundDatas: {},
    cn_name: '',
    fund_id: '',
    daysitem: ['30', '60', '120'],
    lastdays: '30',
    ec: {
      onInit: initChart
    },
  },

  onReady() {
  }
});